<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>使用canvas生成二维码</title>
  <!-- https://github.com/davidshimjs/qrcodejs -->
  <!-- https://mp.weixin.qq.com/s/c8EfdaahkxqRDz9GlJV8XQ -->
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      outline: none;
      border: none;
    }

    :root {
      min-height: 100vh;
    }

    body {
      min-height: inherit;
      background-color: #e9e4e4;
      padding-top: 20px;
    }

    header {
      text-align: center;
    }

    header>h6 {
      color: #aea195;
      font-weight: normal;
      margin-top: 8px;
    }

    .userInput {
      margin-top: 40px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      gap: 20px 0px;
    }

    input {
      padding: 4px 6px;
      border-radius: 2px;
      min-width: 300px;
    }

    button {
      border-radius: 5px;
      padding: 4px 8px;
      background-color: #91b09f;
    }
  </style>
</head>

<body>
  <header>
    <h2>二维码</h2>
    <h6>生成任何二维码</h6>
  </header>
  <section class="userInput">
    <input type="text" id="inputText" autofocus autocomplete="off" placeholder="请输入内容...">
    <button id="button" type="submit">生成二维码</button>
  </section>
  <div id="qrCode"></div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
  "use strict"; {
    let btn = document.getElementById("button")
    let qrCode = document.getElementById("qrCode")

    btn.addEventListener("click", function () {
      let text = this.previousElementSibling.value
      if (!text) return
      console.log(text)

      let res = new QRCode(qrCode, {
        text,
        width: 180, //default 128
        height: 180,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
      })

      console.log(res)

      // let download = document.createElement("button")
      // qrCode.appendChild(download)


      // let download_link = document.createElement("a");
      // download_link.setAttribute("download", "qr_code_linq.png");
      // download_link.innerText = "Download";
      // let a = `<a download="qrCodeLinq.png">下载</a>`
      // download.appendChild(download_link);
    })
  };
</script>

</html>